home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / text / tascii-1.000 / tascii-1 / tascii-1.0 / tascii.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-25  |  8.4 KB  |  276 lines

  1. /* tascii - displays an ASCII character table on standard output.
  2.    Copyright (C) 1996,  Robert A. Biersack
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2 of the License, or
  7.    (at your option) any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18.  
  19. #include <stdio.h>
  20.  
  21.  
  22. /* Translation tables.  */
  23. char *decimal_number[128] =
  24. {"000", "001", "002", "003", "004", "005", "006", "007", "008", "009",
  25.  "010", "011", "012", "013", "014", "015", "016", "017", "018", "019",
  26.  "020", "021", "022", "023", "024", "025", "026", "027", "028", "029",
  27.  "030", "031", "032", "033", "034", "035", "036", "037", "038", "039",
  28.  "040", "041", "042", "043", "044", "045", "046", "047", "048", "049",
  29.  "050", "051", "052", "053", "054", "055", "056", "057", "058", "059",
  30.  "060", "061", "062", "063", "064", "065", "066", "067", "068", "069",
  31.  "070", "071", "072", "073", "074", "075", "076", "077", "078", "079",
  32.  "080", "081", "082", "083", "084", "085", "086", "087", "088", "089",
  33.  "090", "091", "092", "093", "094", "095", "096", "097", "098", "099",
  34.  "100", "101", "102", "103", "104", "105", "106", "107", "108", "109",
  35.  "110", "111", "112", "113", "114", "115", "116", "117", "118", "119",
  36.  "120", "121", "122", "123", "124", "125", "126", "127"};
  37.  
  38. char *octal_number[128] =
  39. {"000", "001", "002", "003", "004", "005", "006", "007",
  40.  "010", "011", "012", "013", "014", "015", "016", "017",
  41.  "020", "021", "022", "023", "024", "025", "026", "027",
  42.  "030", "031", "032", "033", "034", "035", "036", "037",
  43.  "040", "041", "042", "043", "044", "045", "046", "047",
  44.  "050", "051", "052", "053", "054", "055", "056", "057",
  45.  "060", "061", "062", "063", "064", "065", "066", "067",
  46.  "070", "071", "072", "073", "074", "075", "076", "077",
  47.  "100", "101", "102", "103", "104", "105", "106", "107",
  48.  "110", "111", "112", "113", "114", "115", "116", "117",
  49.  "120", "121", "122", "123", "124", "125", "126", "127",
  50.  "130", "131", "132", "133", "134", "135", "136", "137",
  51.  "140", "141", "142", "143", "144", "145", "146", "147",
  52.  "150", "151", "152", "153", "154", "155", "156", "157",
  53.  "160", "161", "162", "163", "164", "165", "166", "167",
  54.  "170", "171", "172", "173", "174", "175", "176", "177"};
  55.  
  56. char *hex_number[128] =
  57. {"00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F",
  58.  "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F",
  59.  "20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F",
  60.  "30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F",
  61.  "40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F",
  62.  "50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F",
  63.  "60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F",
  64.  "70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F"
  65. };
  66.  
  67. char *ascii_char[128] =
  68. {"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
  69.  "BS ", "HT ", "LF ", "VT ", "FF ", "CR ", "SO ", "SI ",
  70.  "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
  71.  "CAN", "EM ", "SUB", "ESC", "FS ", "GS ", "RS ", "US ",
  72.  "SP ", "!  ", "\"  ", "#  ", "$  ", "%  ", "&  ", "'  ",
  73.  "(  ", ")  ", "*  ", "+  ", ",  ", "-  ", ".  ", "/  ",
  74.  "0  ", "1  ", "2  ", "3  ", "4  ", "5  ", "6  ", "7  ",
  75.  "8  ", "9  ", ":  ", ";  ", "<  ", "=  ", ">  ", "?  ",
  76.  "@  ", "A  ", "B  ", "C  ", "D  ", "E  ", "F  ", "G  ",
  77.  "H  ", "I  ", "J  ", "K  ", "L  ", "M  ", "N  ", "O  ",
  78.  "P  ", "Q  ", "R  ", "S  ", "T  ", "U  ", "V  ", "W  ",
  79.  "X  ", "Y  ", "Z  ", "[  ", "\\  ", "]  ", "^  ", "_  ",
  80.  "`  ", "a  ", "b  ", "c  ", "d  ", "e  ", "f  ", "g  ",
  81.  "h  ", "i  ", "j  ", "k  ", "l  ", "m  ", "n  ", "o  ",
  82.  "p  ", "q  ", "r  ", "s  ", "t  ", "u  ", "v  ", "w  ",
  83.  "x  ", "y  ", "z  ", "{  ", "|  ", "}  ", "~  ", "DEL"};
  84.  
  85. /* Name under which this program was invoked.  */
  86. char *program_name;
  87.  
  88. /* Name and version number of program.  */
  89. const char *version_string = "tascii v1.0";
  90.  
  91. char long_option_version[] = "--version";
  92. char long_option_help[] = "--help";
  93.  
  94. void print_table (char *p[]);
  95. int compare (char *s, char *t);
  96. void usage (int status);
  97.  
  98. int
  99. main (int argc, char **argv)
  100. {
  101.   /* Use flags to keep track of options found.  */
  102.   int dflag = 0;
  103.   int oflag = 0;
  104.   int xflag = 0;
  105.   int hflag = 0;
  106.   int vflag = 0;
  107.  
  108.   int i;
  109.   int count;
  110.  
  111.   char *nextchar;
  112.  
  113.   /* Save the program name for future use.  */
  114.      program_name = argv[0];
  115.  
  116.   /* Check for --version or --help.  */
  117.   for (count = 1; count < argc; count++)
  118.     {
  119.       if (compare(long_option_version, argv[count]))
  120.         vflag = 1;
  121.       if (compare(long_option_help, argv[count]))
  122.         hflag = 1;
  123.     }
  124.  
  125.   /* If --version or --help, print message and quit.  */
  126.   if (vflag)
  127.     {
  128.       printf("%s\n", version_string);
  129.       return 0;
  130.     }
  131.  
  132.   if (hflag)
  133.     {
  134.       usage (0);
  135.       return 0;
  136.     }
  137.  
  138.   /* Check for -d -o -x options.  */
  139.   for (count = 1; count < argc; count++)
  140.     {
  141.       nextchar = argv[count];
  142.       if (nextchar[0] != '-')
  143.         {
  144.           /* Missing '-' before the option argument.  */
  145.           fprintf(stderr, "%s: illegal non-option argument -- %s\n",
  146.                   program_name, argv[count]);
  147.           usage(1);
  148.           return 1;
  149.         }
  150.  
  151.       if (nextchar[1] == '\0')
  152.         {
  153.           /* Found '-' without an option argument.  */
  154.           fprintf(stderr, "%s: illegal missing option -- %s\n",
  155.                   program_name, argv[count]);
  156.           usage(1);
  157.           return 1;
  158.         }
  159.  
  160.       if (nextchar[1] == '-')
  161.         {
  162.           /* Found '--'.  */
  163.           break;
  164.         }
  165.  
  166.       for (i = 1; nextchar[i] != '\0'; i++)
  167.         {
  168.           switch (nextchar[i])
  169.             {
  170.               case 'd':{
  171.                 dflag = 1;
  172.                 break;}
  173.               case 'o':{
  174.                 oflag = 1;
  175.                 break;}
  176.               case 'x':{
  177.                 xflag = 1;
  178.                 break;}
  179.               default:{
  180.                 /* Found a non-option.  */
  181.                 fprintf(stderr, "%s: illegal option -- %c\n",
  182.                         program_name, nextchar[i]);
  183.                 usage(1);
  184.                 return 1;}
  185.             }
  186.         }
  187.     }
  188.  
  189.   if (!(oflag || xflag))
  190.     /* Unconditionally set dflag if no other option was selected.  */
  191.     dflag = 1;
  192.  
  193.   if (dflag)
  194.     print_table(decimal_number);
  195.  
  196.   if (oflag)
  197.     print_table(octal_number);
  198.  
  199.   if (xflag)
  200.     print_table(hex_number);
  201.  
  202.   return 0;
  203. }
  204.  
  205.  
  206. /* Print an ASCII table (16 rows, 8 cols) to standard output.  */
  207. void
  208. print_table (char *p[])
  209. {
  210.   int ctr;
  211.   int col0, col1, col2, col3, col4, col5, col6, col7;
  212.  
  213.   printf("\n%41s\n\n","ASCII Character Table");
  214.  
  215.   for (ctr = 0; ctr < 16; ctr++)
  216.     {
  217.       col0 = ctr;
  218.       col1 = ctr + 16;
  219.       col2 = ctr + 32;
  220.       col3 = ctr + 48;
  221.       col4 = ctr + 64;
  222.       col5 = ctr + 80;
  223.       col6 = ctr + 96;
  224.       col7 = ctr + 112;
  225.  
  226.       printf("%3s %3s %4s %3s %4s %3s %3s %3s%3s %3s%3s %3s%3s %3s%3s %3s\n",
  227.               p[col0], ascii_char[col0], p[col1], ascii_char[col1],
  228.               p[col2], ascii_char[col2], p[col3], ascii_char[col3],
  229.               p[col4], ascii_char[col4], p[col5], ascii_char[col5],
  230.               p[col6], ascii_char[col6], p[col7], ascii_char[col7]);
  231.     }
  232.   printf("\n");
  233. }
  234.  
  235. /* Compare two strings and return 1 if they are equal.  */
  236. int
  237. compare (char *s, char *t)
  238. {
  239.   int i;
  240.  
  241.   for (i = 0; s[i] == t[i]; i++)
  242.       if (s[i] == '\0')
  243.         return 1;
  244.   /* If strings are not equal then return 0  */
  245.   return 0;
  246. }
  247.  
  248. /* Print some friendly help information.  */
  249. void
  250. usage (int status)
  251. {
  252.   if (status != 0)
  253.     fprintf (stderr, "Try '%s --help' for more information.\n",
  254.              program_name);
  255.   else
  256.     {
  257.       printf ("\
  258. Usage: %s [OPTION]\n\
  259. ",
  260.               program_name);
  261.       printf ("\
  262. \n\
  263.   -d     decimal notation format\n\
  264.   -o     octal notation format\n\
  265.   -x     hexadecimal notation format\n\
  266. \n\
  267.   --help       display this help and exit\n\
  268.   --version    output version information and exit\n\
  269. \n\
  270. With no options, the default is an ASCII character table in\n\
  271. decimal notation format.\n\
  272. ");
  273.     }
  274. }
  275.  
  276.